#include <QRectF>
#include <QDebug>
#include "mtqgraphicsview.h"

MTQGraphicsView::MTQGraphicsView( QGraphicsScene * scene, QWidget * parent  ):
        QGraphicsView(scene,parent){
    QMap<int,QGraphicsEllipseItem> touches;
}
void MTQGraphicsView::fazBolinha(int touchId, double x, double y, int type){
    QGraphicsScene* s = this->scene();
    ;
    double w = s->width();
    double h = s->height();

    switch (type){
    case 0:
        if(!this->touches.contains(touchId))
            this->touches[touchId] = s->addEllipse(QRectF(30,30,30,30), QPen(QColor("red")), QBrush(QColor("red"), Qt::SolidPattern));
        this->touches[touchId]->setPos((int)x*w,(int)y*h);
        break;
    case 1:
        if(this->touches.contains(touchId)){
            QGraphicsEllipseItem* a = this->touches[touchId];
            a->hide();
            this->touches.remove(touchId);
        }
        break;
    case 2:
        if(this->touches.contains(touchId))
            this->touches[touchId]->setPos(x*w,y*h);
        break;
        
    }
}
void MTQGraphicsView::onTouch(int touchId, double x, double y, int type){
    
    double w = this->rect().width();
    double h = this->rect().height();
    this->fazBolinha(touchId,x,y,type);
    QPoint p = this->mapFromScene(QPointF(x,y));

    QGraphicsItem *item = itemAt(p);
    if (item) {
        MTQGraphicsImage* i = dynamic_cast<MTQGraphicsImage*>(item);
        if(i != 0){
            i->onTouch(touchId,p.x(),p.y(),type);
            qDebug()<<"achou";
        }
    }
    
}
